﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MimiDouban.Abstract;
using MimiDouban.Abstract.Beans;

namespace MimiDouban.UI
{
    /// <summary>
    /// 采用代理模式实现。
    /// 因为Controller只跟一个UI交互，所以要管理许多的InputUI和OutputUI需要一个
    /// 代理类。
    /// </summary>
    public class MainUIProxy:IMainUI
    {
        private List<IInputUI> inputUIs = new List<IInputUI>();
        private List<IOutputUI> outputUIs = new List<IOutputUI>();

        /// <summary>
        /// 添加一个MainUI
        /// </summary>
        /// <param name="newMainUI"></param>
        public void addMainUI(IMainUI newMainUI)
        {
            addInputUI(newMainUI);
            addOutputUI(newMainUI);
            newMainUI.UIStarted += new UIHandler(newMainUI_UIStarted);
        }        

        /// <summary>
        /// 删除一个MainUI
        /// </summary>
        /// <param name="mainUI"></param>
        public void removeMainUI(IMainUI mainUI)
        {
            this.inputUIs.Remove(mainUI);
            this.outputUIs.Remove(mainUI);
        }       

        /// <summary>
        /// 添加一个inputUI
        /// </summary>
        /// <param name="inputUI"></param>
        public void addInputUI(IInputUI inputUI)
        {
            this.inputUIs.Add(inputUI);
            inputUI.ChannelChoosen += new ChannelChooseHandler(inputUI_ChannelChoosen);
            inputUI.DisikeClicked += new ButtonClickHandler(inputUI_DisikeClicked);
            inputUI.LikeClicked += new ButtonClickHandler(inputUI_LikeClicked);
            inputUI.NextClicked += new ButtonClickHandler(inputUI_NextClicked);
            inputUI.PauseClicked += new ButtonClickHandler(inputUI_PauseClicked);
            inputUI.CloseClicked += new ButtonClickHandler(inputUI_CloseClicked);
            inputUI.UserLogin += new UserLoginHandler(inputUI_UserLogin);
            inputUI.VolumeChanged += new VolumeChangedHandler(inputUI_VolumeChanged);
        }         

        /// <summary>
        /// 删除一个inputUI对象
        /// </summary>
        /// <param name="inputUI"></param>
        public void removeInputUI(IInputUI inputUI)
        {
            this.inputUIs.Remove(inputUI);
        }

        /// <summary>
        /// 根据索引删除一个inputui
        /// </summary>
        /// <param name="index"></param>
        public void removeInputUI(int index)
        {
            this.inputUIs.RemoveAt(index);
        }

        /// <summary>
        /// 添加一个outputui
        /// </summary>
        /// <param name="outputUI"></param>
        public void addOutputUI(IOutputUI outputUI)
        {
            this.outputUIs.Add(outputUI);
        }

        /// <summary>
        /// 删除一个outputUI对象
        /// </summary>
        /// <param name="outputUI"></param>
        public void removeOuputUI(IOutputUI outputUI)
        {
            this.outputUIs.Remove(outputUI);
        }

        /// <summary>
        /// 根据索引删除一个outputui
        /// </summary>
        /// <param name="index"></param>
        public void removeOutputUI(int index)
        {
            this.outputUIs.RemoveAt(index);
        }


        #region IInputUI Members

        public event UIHandler UIStarted;

        public event ButtonClickHandler LikeClicked;

        public event ButtonClickHandler DisikeClicked;

        public event ButtonClickHandler NextClicked;

        public event ButtonClickHandler PauseClicked;

        public event ButtonClickHandler CloseClicked;

        public event ChannelChooseHandler ChannelChoosen;

        public event VolumeChangedHandler VolumeChanged;

        public event UserLoginHandler UserLogin;

        #endregion

        #region IOutputUI Members

        public void start()
        {
            foreach (IOutputUI oui in this.outputUIs)
            {
                oui.start();
            }
        }

        public void ShowSongInfo(MimiDouban.Abstract.Beans.DoubanSong song)
        {
            foreach (IOutputUI oui in this.outputUIs)
            {
                oui.ShowSongInfo(song);
            }
        }

        public void ShowChannel(MimiDouban.Abstract.Beans.DoubanChannel channel)
        {
            foreach (IOutputUI oui in this.outputUIs)
            {
                oui.ShowChannel(channel);
            }
        }

        public void ShowNextSong(MimiDouban.Abstract.Beans.DoubanSong song)
        {
            foreach (IOutputUI oui in this.outputUIs)
            {
                oui.ShowNextSong(song);
            }
        }

        public void showBuffer(string str)
        {
            foreach (IOutputUI oui in this.outputUIs)
            {
                oui.showBuffer(str);
            }

        }

        public void showTime(int min, int sec)
        {
            foreach (IOutputUI oui in this.outputUIs)
            {
                oui.showTime(min, sec);
            }
        }

        public void showVolume(double volume)
        {
            foreach (IOutputUI oui in this.outputUIs)
            {
                oui.showVolume(volume);
            }
        }

        public void setLikeStatus(ControlStatus status)
        {
            foreach (IOutputUI oui in this.outputUIs)
            {
                oui.setLikeStatus(status);
            }
        }

        public ControlStatus getLikeStatus()
        {
            if (this.outputUIs.Count != 0)
            {
                return this.outputUIs[0].getLikeStatus();
            }
            else
            {
                return ControlStatus.Enabled;
            }
        }

        public void setDislikeStatus(ControlStatus status)
        {
            foreach (IOutputUI oui in this.outputUIs)
            {
                oui.setDislikeStatus(status);
            }
        }

        public void setPauseStatus(bool status)
        {
            foreach (IOutputUI oui in this.outputUIs)
            {
                oui.setPauseStatus(status);
            }
        }

        public void showLogin()
        {
            foreach (IOutputUI oui in this.outputUIs)
            {
                oui.showLogin();
            }
        }

        public void showCurrentUser(string username)
        {
            foreach (IOutputUI oui in this.outputUIs)
            {
                oui.showCurrentUser(username);
            }
        }

        public void showLoginBtn()
        {
            foreach (IOutputUI oui in this.outputUIs)
            {
                oui.showLoginBtn();
            }
        }

        public MimiDouban.Abstract.Beans.DoubanChannel[] Channels
        {
            get
            {
                foreach (IOutputUI oui in this.outputUIs)
                {
                    if (oui.Channels != null)
                    {
                        return oui.Channels;
                    }
                }
                return null;
            }
            set
            {
                foreach (IOutputUI oui in this.outputUIs)
                {
                    oui.Channels = value;
                }
            }
        }

        public void end()
        {
            foreach (IOutputUI oui in this.outputUIs)
            {
                oui.end();
            }
            foreach (IInputUI iui in this.inputUIs)
            {
                iui.end();
            }
        }

        #endregion

        #region Private Event Handler

        private void newMainUI_UIStarted()
        {
            if (this.UIStarted != null)
            {
                this.UIStarted.Invoke();
            }
        }

        private void inputUI_VolumeChanged(double newVolume)
        {
            if (this.VolumeChanged != null)
            {
                this.VolumeChanged.Invoke(newVolume);
            }
        }

        private DoubanUser inputUI_UserLogin(string email, string password)
        {
            if (this.UserLogin != null)
            {
                return this.UserLogin.Invoke(email,password);
            }
            return null;
        }

        private void inputUI_CloseClicked()
        {
            if (this.CloseClicked != null)
            {
                this.CloseClicked.Invoke();
            }
        }

        private void inputUI_PauseClicked()
        {
            if (this.PauseClicked != null)
            {
                this.PauseClicked.Invoke();
            }
        }

        private void inputUI_NextClicked()
        {
            if (this.NextClicked != null)
            {
                this.NextClicked.Invoke();
            }
        }

        private void inputUI_LikeClicked()
        {
            if (this.LikeClicked != null)
            {
                this.LikeClicked.Invoke();
            }
        }

        private void inputUI_DisikeClicked()
        {
            if (this.DisikeClicked != null)
            {
                this.DisikeClicked.Invoke();
            }
        }

        private void inputUI_ChannelChoosen(int channelId)
        {
            if (this.ChannelChoosen != null)
            {
                this.ChannelChoosen.Invoke(channelId);
            }
        }
        #endregion
    }
}
